package com.jiangyg.mall.admin.controller;

import com.jiangyg.mall.admin.dto.ResourceDTO;
import com.jiangyg.mall.admin.vo.ResourceVO;
import com.jiangyg.mall.admin.service.ResourceService;
import com.jiangyg.mall.authz.UserInfoHolder;
import com.jiangyg.mall.core.support.restful.Result;
import com.jiangyg.mall.core.utils.tree.Tree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 类描述：资源管理控制器
 *
 * @author jiangyg
 * @version 2.0
 * @date 2020-05-31
 */
@RestController
@RequestMapping("/resource")
public class ResourceController {

    /**
     * 资源接口
     */
    private final ResourceService resourceService;

    @Autowired
    public ResourceController(ResourceService resourceService) {
        this.resourceService = resourceService;
    }

    /**
     * 功能描述：获取用户授权菜单树形结构
     *
     * @param request  请求
     * @param response 响应
     * @param type     查询类型，1-仅查询菜单、目录和页面 2-查询所有资源
     * @return 结果
     */
    @ResponseBody
    @GetMapping("/tree")
    public Result<List<Tree>> tree(HttpServletRequest request, HttpServletResponse response,
                                   @RequestParam(name = "type", defaultValue = "1") String type) throws Exception {
        final Long userId = UserInfoHolder.isAdmin() ? null : UserInfoHolder.loginUserId();
        final List<Tree> menus = resourceService.selectTree(type, userId);
        return Result.ok(menus);
    }

    /**
     * 功能描述：获取登录用户授权资源列表
     *
     * @param request  请求
     * @param response 响应
     * @param pid      父级ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/list")
    public Result<List<ResourceVO>> list(HttpServletRequest request, HttpServletResponse response,
                                         @RequestParam(name = "pid", required = false) Long pid) throws Exception {
        final Long userId = UserInfoHolder.isAdmin() ? null : UserInfoHolder.loginUserId();
        return Result.ok(resourceService.selectListByPid(pid, userId));
    }

    /**
     * 功能描述：保存或者修改资源信息
     *
     * @param request 请求
     * @param dto     资源信息
     * @return 结果
     * @throws Exception 异常
     */
    @ResponseBody
    @PostMapping("/save")
    public Result<Object> save(HttpServletRequest request, @RequestBody @Validated ResourceDTO dto) throws Exception {
        return Result.saveOrUpdate(resourceService.saveOrUpdate(dto));
    }

    /**
     * 功能描述：根据ID获取资源信息
     *
     * @param request    请求
     * @param resourceId 资源ID
     * @return 结果
     * @throws Exception 异常
     */
    @ResponseBody
    @PostMapping("/info")
    public Result<ResourceVO> info(HttpServletRequest request, @RequestParam(name = "resourceId") Long resourceId) throws Exception {
        return Result.ok(resourceService.selectById(resourceId));
    }

    /**
     * 功能描述：删除资源信息
     *
     * @param request     请求
     * @param resourceIds 批量删除的资源ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/delete")
    public Result<Object> delete(HttpServletRequest request, @RequestBody List<Long> resourceIds) {
        return Result.delete(resourceService.delete(resourceIds));
    }

}
